#include "elk_main_wnd.h"
#include "ui_elk_main_wnd.h"
#include "elk_common_dlg.h"
#include <QKeyEvent>

ElkMainWnd::ElkMainWnd(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ElkMainWnd)
{
    ui->setupUi(this);

    qRegisterMetaType<ElkRequestDetails>("ElkRequestDetails");
    qRegisterMetaType<ElkContextMenuDetails>("ElkContextMenuDetails");
}

ElkMainWnd::~ElkMainWnd()
{
    delete ui;
}

void ElkMainWnd::onWebTitleChanged(const QString &title)
{
    this->setWindowTitle(title);
}

void ElkMainWnd::onWebScreenModeChanged(bool fullscreen)
{
    if (fullscreen) {
        showFullScreen();
    } else {
        showNormal();
    }
}

void ElkMainWnd::onContextMenuShowDetails(const QString &frame,
                                          const ElkContextMenuDetails &details)
{
    QStringList list;
    list << QString("context: ") + details.contextMenuType;
    list << QString("charset: ") + details.frameCharset << QString();

    if (!details.pageUrl.isEmpty()) {
        list << QString("page url: ");
        list << details.pageUrl << QString();
    }
    if (!details.frameUrl.isEmpty()) {
        list << QString("frame url: ");
        list << details.frameUrl << QString();
    }
    if (!details.linkUrl.isEmpty()) {
        list << QString("link url: ");
        list << details.linkUrl << QString();
    }
    if (!details.mediaSourceUrl.isEmpty()) {
        list << details.contextMenuMediaType + QString(": ") + details.mediaTitleText;
        list << details.mediaSourceUrl << QString();
    }
    if (!details.selectionText.isEmpty()) {
        list << QString("selection: ");
        list << details.selectionText << QString();
    }

    if (frame.isEmpty()) {
        ElkCommonDlg::showTextBrowserDlg(this->window(), QString("Main Frame"), list);
    } else {
        ElkCommonDlg::showTextBrowserDlg(this->window(), frame, list);
    }
}

void ElkMainWnd::onContextMenuDownloadMedia(const QString &frame,
                                            const ElkContextMenuDetails &details)
{
    Q_UNUSED(frame);
    if (downloadView_) {
        ui->tabWidget->setCurrentWidget(downloadView_);
        downloadView_->request(details.mediaSourceUrl, QString());
    }
}

void ElkMainWnd::onContextMenuViewPageSource(const QString &frame,
                                             const ElkContextMenuDetails &details)
{
    Q_UNUSED(frame);
    Q_UNUSED(details);
    if (previewView_) {
        ui->tabWidget->setCurrentWidget(previewView_);
    }
}

void ElkMainWnd::onContextMenuViewDisplayText(const QString &frame,
                                              const ElkContextMenuDetails &details)
{
    Q_UNUSED(frame);
    Q_UNUSED(details);
    if (previewView_) {
        ui->tabWidget->setCurrentWidget(previewView_);
    }
}

void ElkMainWnd::showEvent(QShowEvent *event)
{
    Q_UNUSED(event);
    if (ui->tabWidget->count() == 0) {
        showWebView();
        showDownloadView();
        showResourceView();
        showPreviewView();
        ui->tabWidget->setCurrentWidget(webView_);
    }
}

void ElkMainWnd::hideEvent(QHideEvent *event)
{
    Q_UNUSED(event);
}

void ElkMainWnd::showWebView()
{
    if (!webView_) {
        webView_ = new ElkWebView();
        webDelegate_ = webView_->delegate();

        connect(webDelegate_.data(), SIGNAL(titleChanged(const QString &)),
                this, SLOT(onWebTitleChanged(const QString &)));
        connect(webDelegate_.data(), SIGNAL(fullscreenModeChanged(bool)),
                this, SLOT(onWebScreenModeChanged(bool)));

        connect(webDelegate_.data(), SIGNAL(contextMenuShowDetails(const QString &,
                                                                   const ElkContextMenuDetails &)),
                this, SLOT(onContextMenuShowDetails(const QString &,
                                                    const ElkContextMenuDetails &)));
        connect(webDelegate_.data(), SIGNAL(contextMenuDownloadMedia(const QString &,
                                                                     const ElkContextMenuDetails &)),
                this, SLOT(onContextMenuDownloadMedia(const QString &,
                                                      const ElkContextMenuDetails &)));

        connect(webDelegate_.data(), SIGNAL(contextMenuViewDisplayText(const QString &,
                                                                       const ElkContextMenuDetails &)),
                this, SLOT(onContextMenuViewDisplayText(const QString &,
                                                        const ElkContextMenuDetails &)));
        connect(webDelegate_.data(), SIGNAL(contextMenuViewPageSource(const QString &,
                                                                      const ElkContextMenuDetails &)),
                this, SLOT(onContextMenuViewPageSource(const QString &,
                                                       const ElkContextMenuDetails &)));

        ui->tabWidget->addTab(webView_, tr("web"));
    }
}

void ElkMainWnd::showDownloadView()
{
    if (!downloadView_ && webView_) {
        downloadView_ = new ElkDownloadView();
        downloadView_->bind(webView_->delegate());
        ui->tabWidget->addTab(downloadView_, tr("downloads"));
    }
}

void ElkMainWnd::showResourceView()
{
    if (!resourceView_ && webView_) {
        resourceView_ = new ElkResourceView();
        resourceView_->bind(webView_->delegate());
        ui->tabWidget->addTab(resourceView_, tr("resources"));
    }
}

void ElkMainWnd::showPreviewView()
{
    if (!previewView_ && webView_) {
        previewView_ = new ElkPreviewView();
        previewView_->bind(webView_->delegate());
        ui->tabWidget->addTab(previewView_, tr("preview"));
    }
}
